<?php
/**
 *海报
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/23
 *  use service\HttpService;
 *  return HttpService::getRequest("URL",$param);
 */
namespace base\files;
use think\Image;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Response\QrCodeResponse;

// use base\files\Poster;
// 生成海报		Poster::generate();	
// 生成二维码		Poster::qrCode();	

class Poster {
	
	/**
    * 生成
    * @param string $msg
    * @param string $url
    * @param array $data
    * @return array
    */
	public function generate($bg= "",$url,$size=50, $x= 10,$y=10,$is_res_url=false){
		//临时文件地址
		$path = public_path().'/images/bill/poster/';
		//生成二维码
		$qrCode = self::qrCode($url,$size);
		//生成海报
		copy($bg,$path.'fx-01.jpg');
		$image = Image::open($path.'fx-01.jpg');
		$image->water($path.'qrcode.png',[$x,$y])->save($path.'water_image.png'); 
		if(!$is_res_url){
			Header("Content-type: image/png"); 
			die(imagejpeg( imagecreatefromjpeg($path.'water_image.png') ));
		}else{
			return $path.'water_image.png';
		}
	}
	
	
	/**
    * 生成二维码
    * @param string $msg
    * @param string $url
    * @param array $data
    * @return array
    */
	public function qrCode ($url,$size= 50,$type=''){
		$path = public_path().'/images/bill/poster/';
		$qrCode = new QrCode($url);
        $qrCode->setSize( $size );
        $qrCode->setWriterByName('png');
        $qrCode->setMargin(10);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
        $qrCode->setLogoSize(150, 200);
        $qrCode->setRoundBlockSize(true);
        $qrCode->setValidateResult(false);
        $qrCode->setWriterOptions(['exclude_xml_declaration' => true]);
        $qrCode->writeFile($path.$type.'_qrcode.png');
        if($type=='coupon'){
        	return APP_PATH.'/images/bill/poster/'.$type.'_qrcode.png?t='.time();
        }
		return $path.$type.'_qrcode.png';
	}
	
	
}